package com.zhizhi.module.synthesizes.account.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.zhizhi.common.base.BaseServiceImpl;
import com.zhizhi.common.constant.SystemConstant;
import com.zhizhi.domain.AccountBO;
import com.zhizhi.domain.RequestTokenBO;
import com.zhizhi.module.synthesizes.account.entity.Account;
import com.zhizhi.module.synthesizes.account.mapper.AccountMapper;
import com.zhizhi.module.synthesizes.account.service.AccountService;
import com.zhizhi.util.EasyBeanUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Yu
 * @since 2021-01-17
 */
@Service
public class AccountServiceImpl extends BaseServiceImpl<AccountMapper, Account> implements AccountService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 根据token获取对应的用户
     * @param token
     * @return
     */
    public RequestTokenBO getAccountByToken(String token) {
        String tokenKey = SystemConstant.KEY_TOKEN + token;
        Object accountIdObject = redisTemplate.opsForValue().get(tokenKey);
        if (ObjectUtil.isNotNull(accountIdObject)) {
            Integer accountId = Integer.parseInt(accountIdObject.toString());
            Account account = selectById(accountId);
            if (ObjectUtil.isNotNull(account)) {
                AccountBO accountBO = EasyBeanUtil.copy(account, AccountBO.class);
                if (ObjectUtil.isNotNull(accountBO)) {
                    return new RequestTokenBO(token, accountBO);
                }
            }
        }
        return null;
    }

    @Override
    public String loginByCode(String code) {


        return null;
    }

}